package com.link_with_health.service.impl;

import com.link_with_health.exception.BaseException;
import com.link_with_health.exception.ModifyDatabaseFailedException;
import com.link_with_health.exception.NullQueryResultException;
import com.link_with_health.mapper.DoctorMapper;
import com.link_with_health.mapper.DoctorStatusMapper;
import com.link_with_health.mapper.DrTitleMapper;
import com.link_with_health.model.DoctorEntity;
import com.link_with_health.model.DoctorStatusEntity;
import com.link_with_health.model.DrTitleEntity;
import com.link_with_health.service.BaseService;
import com.link_with_health.service.DoctorService;
import org.apache.ibatis.jdbc.Null;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("doctorService")
public class DoctorServiceImpl extends BaseService implements DoctorService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    DoctorMapper doctorMapper;
    @Resource
    DrTitleMapper drTitleMapper;
    @Resource
    DoctorStatusMapper doctorStatusMapper;

    /**
     * 通过医生id获取医生信息
     * @param drId 医生id
     * @return 医生信息
     * @throws BaseException 异常
     */
    @Override
    public DoctorEntity getDoctorsByDrId(Long drId) throws BaseException {
        DoctorEntity entity = null;
        try {
            entity = doctorMapper.queryDoctorsByDrId(drId);
        } catch (Exception e) {
            logger.error("[query doctor failed]",e);
        }
        if(entity == null)
            throw new NullQueryResultException("DOCTOR.DR_ID: "+drId);

        return entity;
    }

    /**
     * 通过用户id获取医生信息
     * @param userId 用户id
     * @return 医生信息
     * @throws BaseException 异常
     */
    @Override
    public DoctorEntity getDoctorsByUserId(Long userId) throws BaseException {
        DoctorEntity entity = null;
        try {
            entity = doctorMapper.queryDoctorsByUserId(userId);
        } catch (Exception e) {
            logger.error("[query doctor failed]",e);
        }
        if(entity == null)
            throw new NullQueryResultException("DOCTOR.USER_ID: "+userId);

        return entity;
    }

    /**
     * 通过医生名字获取医生信息
     * @param realName 名字
     * @return 医生信息
     * @throws BaseException 异常
     */
    @Override
    public List<DoctorEntity> getDoctorsByRealName(String realName) throws BaseException {
        List<DoctorEntity> doctorList = null;
        try {
            doctorList = doctorMapper.queryDoctorsByRealName("%"+realName+"%");
        } catch(Exception e){
            logger.error("[query doctor failed]",e);
        }

        if (doctorList == null || doctorList.isEmpty())
            throw new NullQueryResultException("DOCTOR.REAL_NAME: " + realName);
        return doctorList;
    }

    /**
     * 通过医院获取医生信息
     * @param hospitalId 公司id
     * @return 医生信息
     * @throws BaseException 异常
     */
    @Override
    public List<DoctorEntity> getDoctorsByHospital(Long hospitalId) throws BaseException {
        List<DoctorEntity> doctorList = null;
        try {
            doctorList = doctorMapper.queryDoctorsByHospitalId(hospitalId);
        } catch (Exception e) {
            logger.error("[query doctor failed]",e);
        }
        if(doctorList == null || doctorList.isEmpty())
            throw new NullQueryResultException("DOCTOR.HOSPITAL_ID: "+hospitalId);

        return doctorList;
    }

    /**
     * 通过职称获取医生信息
     * @param titleId 职称id
     * @return 医生信息
     * @throws BaseException 异常
     */
    @Override
    public List<DoctorEntity> getDoctorsByTitle(int titleId) throws BaseException {
        List<DoctorEntity> doctorList = null;
        try {
            doctorList = doctorMapper.queryDoctorsByTitle(titleId);
        } catch (Exception e) {
            logger.error("[query doctor failed]",e);
        }
        if(doctorList == null || doctorList.isEmpty())
            throw new NullQueryResultException("DOCTOR.TITLE: "+titleId);

        return doctorList;
    }

    /**
     * 通过科室id获取医生信息 包含父级科室
     * @param departmentId 科室id
     * @return 医生信息
     * @throws BaseException 异常
     */
    @Override
    public List<DoctorEntity> getDoctorsByDepartmentIncPareDepart(Long departmentId) throws BaseException {
        List<DoctorEntity> doctorList = null;
        try {
            doctorList = doctorMapper.queryDoctorsIncludeParentDeparts(departmentId);

        } catch (Exception e) {
            logger.error("[query doctor failed]",e);
        }
        if(doctorList == null || doctorList.isEmpty())
            throw new NullQueryResultException("DOCTOR.DEPARTMENT_ID" +departmentId);

        return doctorList;
    }

    /**
     * 通过科室id获取医生信息
     * @param departmentId 科室id
     * @return 医生信息
     * @throws BaseException 异常
     */
    @Override
    public List<DoctorEntity> getDoctorsByDepartment(Long departmentId) throws BaseException {
        List<DoctorEntity> doctorList = null;
        try {
            doctorList = doctorMapper.queryDoctorsByDepartment(departmentId);
        } catch (Exception e) {
            logger.error("[query doctor failed]",e);
        }
        if (doctorList == null || doctorList.isEmpty())
            throw new NullQueryResultException("DOCTOR.DEPARTMENT_ID: "+departmentId);
        return doctorList;
    }

    /**
     * 通过状态筛选医生信息
     * @param status 状态id
     * @return 医生信息
     * @throws BaseException 异常
     */
    @Override
    public List<DoctorEntity> getDoctorsByStatus(int status) throws BaseException {
        List<DoctorEntity> doctorList = null;
        try {
            doctorList = doctorMapper.queryDoctorsByStatus(status);
        } catch (Exception e){
            logger.error("[query doctor failed]",e);
        }
        if(doctorList == null || doctorList.isEmpty())
            throw new NullQueryResultException("DOCTOR.DOCTOR_STATUS: "+status);
        return doctorList;
    }

    /**
     * 获取医生信息
     * @return 医生信息
     * @throws BaseException 异常
     */
    @Override
    public List<DoctorEntity> getAllDoctors() throws BaseException {
        List<DoctorEntity> doctorList = null;

        try {
            doctorList = doctorMapper.queryAllDoctors();
        } catch (Exception e) {
            logger.error("[query doctor failed]",e);
        }
        if (doctorList == null || doctorList.isEmpty())
            throw new NullQueryResultException("ALL DOCTOR");
        return doctorList;
    }

    @Override
    public List<DoctorEntity> searchDoctorsByNameOrHospitalName(String doctorName, String hospitalName)
            throws BaseException {
        List<DoctorEntity> doctorList = null;
        try {
            doctorList = doctorMapper
                    .queryDoctorsByNameOrHospitalName("%"+doctorName+"%"
                            ,"%"+hospitalName+"%");
        } catch(Exception e){
            logger.error("[query doctor failed ]",e);
        }
        if (doctorList == null || doctorList.isEmpty())
            throw new NullQueryResultException(" DOCTOR.REAL_NAME " +
                    "OR HOSPITAL.HOSPITAL_NAME "+doctorName + " " +hospitalName );
        return doctorList;
    }

    /**
     * 修改医生信息
     * @param doctor 医生信息
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int changeDoctor(DoctorEntity doctor) throws BaseException {
        int result = 0;
        try {
            result = doctorMapper.updateDoctor(doctor);
        } catch (Exception e) {
            logger.error("[change doctor failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("UPDATE DOCTOR: "+doctor);

        return result;
    }

    /**
     * 增加医生信息
     * @param doctor 医生信息
     * @return 改动数量
     * @throws BaseException
     */
    @Override
    public int addDoctor(DoctorEntity doctor) throws BaseException {
        int result = 0;
        try {
            result = doctorMapper.insertDoctor(doctor);
        } catch (Exception e) {
            logger.error("[add doctor failed]",e);
        }
        if(result == 0)
            throw new ModifyDatabaseFailedException("INSERT DOCTOR: "+doctor);

        return result;
    }

    /**
     * 删除医生信息
     * @param DrId 医生id
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int deleteDoctor(Long DrId) throws BaseException {
        int result = 0;
        try {
            result = doctorMapper.deleteDoctor(DrId);
        } catch (Exception e) {
            logger.error("[delete doctor failed]",e);
        }
        if(result == 0)
            throw new ModifyDatabaseFailedException("DELETE DOCTOR.DR_ID: "+DrId);

        return result;
    }

    /**
     * 通过获取状态信息
     * @param id 状态id
     * @return 状态信息
     * @throws BaseException 异常
     */
    @Override
    public DoctorStatusEntity getStatusById(int id) throws BaseException {
        DoctorStatusEntity entity = null;
        try {
            entity = doctorStatusMapper.queryStatusById(id);
        } catch (Exception e) {
            logger.error("[query doctorStatus failed]",e);
        }
        if (entity == null)
            throw new NullQueryResultException("DOCTORSTATUS.ID: "+id);
        return entity;
    }

    /**
     * 通过状态名字获取状态信息
     * @param status 状态
     * @return 状态信息
     * @throws BaseException 异常
     */
    @Override
    public DoctorStatusEntity getStatusByStatus(String status) throws BaseException {
        DoctorStatusEntity entity = null;
        try {
            entity = doctorStatusMapper.queryStatusByStatus(status);
        } catch (Exception e) {
            logger.error("[query doctorStatus failed]",e);
        }
        if (entity == null)
            throw new NullQueryResultException("DOCTORSTATUS.STATUS: "+status);

        return entity;
    }

    /**
     * 通过医生id获取状态信息
     * @param doctorId id
     * @return 状态信息
     * @throws BaseException 异常
     */
    @Override
    public DoctorStatusEntity getStatusByDoctor(Long doctorId) throws BaseException {
        DoctorStatusEntity entity = null;
        try {
            entity = doctorStatusMapper.queryStatusByDoctor(doctorId);
        } catch (Exception e) {
            logger.error("[query doctorStatus failed]",e);
        }
        if (entity == null)
            throw new NullQueryResultException("DOCTOR.DR_ID: "+doctorId);
        return entity;
    }

    /**
     * 获取所有状态信息
     * @return 状态信息
     * @throws BaseException 异常
     */
    @Override
    public List<DoctorStatusEntity> getAllStatus() throws BaseException {
        List<DoctorStatusEntity> statusList = null;
        try {
            statusList = doctorStatusMapper.queryAllStatus();
        } catch (Exception e) {
            logger.error("[query doctorStatus failed]",e);
        }
        if(statusList == null || statusList.isEmpty())
            throw new NullQueryResultException("ALL DOCTORSTATUS ");
        return statusList;
    }

    /**
     * 更改状态信息
     * @param status 状态信息
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int changeStatus(DoctorStatusEntity status) throws BaseException {
        int result = 0;
        try {
            result = doctorStatusMapper.updateStatus(status);
        } catch (Exception e) {
            logger.error("[change doctorStatus failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("UPDATE DOCTORSTATUS: "+status);

        return result;
    }

    /**
     * 增加状态信息
     * @param status 状态信息
     * @return  改动数量
     * @throws BaseException 异常
     */
    @Override
    public int addStatus(DoctorStatusEntity status) throws BaseException {
        return 0;
    }

    /**
     * 删除状态信息
     * @param statusId 状态信息
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int deleteStatus(int statusId) throws BaseException {
        int result = 0;
        try {
            if(this.doctorMapper.queryDoctorsByStatus(statusId).isEmpty())
                result = doctorStatusMapper.deleteStatus(statusId);
        } catch (Exception e) {
            logger.error("[delete doctorStatus failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("DELETE DOCTORSTATUS.STATUS: "+statusId);
        return result;
    }

    /**
     * 通过职称id获取职称信息
     * @param id 职称id
     * @return 职称信息
     * @throws BaseException 异常
     */
    @Override
    public DrTitleEntity getTitleById(int id) throws BaseException {
        DrTitleEntity entity = null;
        try {
            entity = drTitleMapper.queryTitleById(id);

        } catch (Exception e) {
            logger.error("[query drTitle failed]",e);
        }

        if (entity == null)
            throw new NullQueryResultException("DRTITLE.ID: "+id);
        return entity;
    }

    /**
     * 根据职称获取职称信息
     * @param titleName 职称名字
     * @return 职称信息
     * @throws BaseException 异常
     */
    @Override
    public DrTitleEntity getTitleByName(String titleName) throws BaseException {
        DrTitleEntity entity = null;
        try {
            entity = drTitleMapper.queryTitleByName(titleName);
        } catch (Exception e) {
            logger.error("[query DrTitle failed]",e);
        }
        if (entity == null)
            throw new NullQueryResultException("DRTITLE.NAME: "+titleName);

        return entity;
    }

    /**
     * 通过医生信息获取职称信息
     * @param doctorId 医生id
     * @return 职称信息
     * @throws BaseException 异常
     */
    @Override
    public DrTitleEntity getTitleByDoctorId(Long doctorId) throws BaseException {
        DrTitleEntity entity = null;
        try {
            entity = drTitleMapper.queryTitleByDoctorId(doctorId);
        } catch (Exception e){
            logger.error("[query DrTitle failed]",e);
        }
        if(entity == null)
            throw new NullQueryResultException("QUERY DRTITLE BY DOCTOR.DR_ID: "+doctorId);

        return entity;
    }

    /**
     * 获取所有职称信息
     * @return 职称信息
     * @throws BaseException 异常
     */
    @Override
    public List<DrTitleEntity> getAllTitle() throws BaseException {
        List<DrTitleEntity> titleList = null;
        try {
            titleList = drTitleMapper.queryAllTitle();
        } catch (Exception e) {
            logger.error("[query DrTitle failed]",e);
        }
        if(titleList == null || titleList.isEmpty())
            throw new NullQueryResultException("ALL DRTITLE ");
        return titleList;
    }

    /**
     * 更改职称信息
     * @param title 职称信息
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int changeTitle(DrTitleEntity title) throws BaseException {
        int result = 0;
        try {
            result = drTitleMapper.updateTitle(title);
        } catch (Exception e) {
            logger.error("[change drTitle failed]");
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("UPDATE DRTITLE: "+title);
        return 0;
    }

    /**
     * 增加职称信息
     * @param title 职称信息
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int addTitle(DrTitleEntity title) throws BaseException {
        int result = 0;
        try {
            result = drTitleMapper.insertTitle(title);
        } catch (Exception e) {
            logger.error("[add drTitle failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("INSERT DRTITLE: "+title);
        return result;
    }

    /**
     * 删除职称信息
     * @param titleId 职称
     * @return 改动数量
     * @throws BaseException
     */
    @Override
    public int deleteTitle(int titleId) throws BaseException {
        int result = 0;
        try {
            if(this.doctorMapper.queryDoctorsByTitle(titleId).isEmpty())
                result = drTitleMapper.deleteTitle(titleId);
        } catch (Exception e) {
            logger.error("[delete dtTitle failed]");
        }
        if(result == 0)
            throw new ModifyDatabaseFailedException("DELETE DRTITLE.ID: "+titleId);
        return result;
    }
}
